package com.kayo.specimen.service;

import com.kayo.specimen.dao.*;
import com.kayo.specimen.model.*;
import com.kayo.specimen.model.view.UserView;
import com.kayo.specimen.utility.CryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userMapper")
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Override
    public int addUsers(User... users) throws NoSuchAlgorithmException {
        int count = 0;
        for (User user : users) {
            if (user.getName().isEmpty()) {
                throw new InvalidParameterException("名称不能为空。");
            }

            if (user.getCode().isEmpty()) {
                throw new InvalidParameterException("代码不能为空。");
            }

            if (user.getPassword().isEmpty()) {
                throw new InvalidParameterException("密码不能为空。");
            }

            user.setId(UUID.randomUUID().toString());

            System.out.println("=======加密前的数据:" + user.getPassword());
            String encrypt = CryptUtils.encrypt(user.getPassword());
            user.setPassword(encrypt);

            count += userMapper.insert(users);
        }
        return count;
    }

    @Override
    public int removeUsers(User... users) {
        int count = 0;
        for (User user : users) {
            if (user.getId().isEmpty())
                throw new InvalidParameterException("用户ID不能少于等于零。");
            userMapper.delete(user.getId());
        }
        return count;
    }

    @Override
    public int removeUsers(String... userId) {
        return userMapper.delete(userId);
    }

    @Override
    public List<UserView> getUsers(String code, String name, String email, String order) {
        List<UserView> users = userMapper.select(code, name, email, order);
        users.forEach(f->f.setPassword(CryptUtils.decrypt(f.getPassword())));
        return users;
    }

    @Override
    public int updateUsers(User... users) {
        for (User user : users) {
            user.setPassword(CryptUtils.encrypt(user.getPassword()));
        }
        return userMapper.update(users);
    }

    @Override
    public int grantRoles(String userId, String... roles) {
        int count = 0;
        for (String roleId : roles) {
            UserRole userRole = new UserRole(UUID.randomUUID().toString(), userId, roleId);
            userRoleMapper.insert(userRole);
            count++;
        }

        return count;
    }

    @Override
    public int revokeRoles(String userId, String... roles) {
        int count = 0;
        for (String roleId : roles) {
            count += userRoleMapper.deleteByUserAndRole(userId, roleId);
        }

        return count;
    }

    @Override
    public int revokeRoles(UserRole... userRoles){
        int count = 0;
        for (UserRole userRole : userRoles) {
            count += userRoleMapper.delete(userRole.getId());
        }

        return count;
    }

    @Override
    public List<Role> getGrantedRoles(String userId) {
        return userRoleMapper.getRoles(userId);
    }

    @Override
    public List<Menu> getGrantedMenus(String userId) {
        return menuMapper.getMenusByUser(userId, null);
    }

    @Override
    public List<Privilege> getGrantedPrivileges(String userId) {
        return privilegeMapper.getPrivilegeByUser(userId);
    }
}
